import { MenuItemDefinition } from '@amcax/base';
import { ProductTreeNode } from '../../types';
import { cmdViewConstrains } from '@app-cad-env/product/commands/cmd-view-constraints';

export const viewAssemblyConstraints: MenuItemDefinition<ProductTreeNode> = {
  id: 'view-assembly-constraints',
  children: '查看配合',
  hidden: ({ data: { docTree }, props }) => {
    const nodeTypesWithConstraints = ['product', 'part'];
    // IAssemblyDocTreeNode
    if (props?.type) {
      return !nodeTypesWithConstraints.includes(props.type);
    }

    // selected keys
    if (Array.isArray(props)) {
      const allNodes = props
        .map((item) => docTree.children.find(({ key }) => key === item))
        .filter(Boolean);

      return allNodes.some(
        ({ type }) => !nodeTypesWithConstraints.includes(type),
      );
    }

    return true;
  },
  onClick: async ({ data: { invokeCmd }, props }) => {
    invokeCmd(cmdViewConstrains, props);
  },
};
